/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package quests.Q264_KeenClaws;

import net.sf.l2j.gameserver.model.actor.L2Npc;
import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
import net.sf.l2j.gameserver.model.quest.Quest;
import net.sf.l2j.gameserver.model.quest.QuestState;
import net.sf.l2j.gameserver.model.quest.State;

public class Q264_KeenClaws extends Quest
{
	private final static String qn = "Q264_KeenClaws";
	
	//Item
	private static final int WOLF_CLAW = 1367;
	
	//NPC
	private static final int PAYNE = 30136;
	
	//Mobs
	private static final int GOLBIN = 20003;
	private static final int WOLF = 20456;
	
	public Q264_KeenClaws(int questId, String name, String descr)
	{
		super(questId,name,descr);
		
		questItemIds = new int[] { WOLF_CLAW };
		
		addStartNpc(PAYNE);
		addTalkId(PAYNE);
		
		addKillId(GOLBIN, WOLF);
	}
	
	@Override
	public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
	{
		String htmltext = event;
		QuestState st = player.getQuestState(qn);
		if (st == null)
			return htmltext;
		
		if (event.equalsIgnoreCase("30136-03.htm"))
		{
			st.set("cond", "1");
			st.setState(State.STARTED);
			st.playSound("ItemSound.quest_accept");
		}
		
		return htmltext;
	}
	
	@Override
	public String onTalk(L2Npc npc, L2PcInstance player)
	{
		String htmltext = Quest.getNoQuestMsg(player);
		QuestState st = player.getQuestState(qn);
		if (st == null)
			return htmltext;
		
		switch (st.getState())
		{
		case State.CREATED:
			if (st.getPlayer().getLevel() >= 3 && st.getPlayer().getLevel() <= 9)
				htmltext = "30136-02.htm";
			else
			{
				htmltext = "30136-01.htm";
				st.exitQuest(true);
			}
			break;
			
		case State.STARTED:
			int count = st.getQuestItemsCount(WOLF_CLAW);
			
			if (count < 50)
				htmltext = "30136-04.htm";
			else
			{
				st.takeItems(WOLF_CLAW, -1);
				
				if (st.getRandom(17) < 6)
					st.rewardItems(5140, 1);
				else
					st.rewardItems(734, 1);
				
				htmltext = "30136-05.htm";
				st.exitQuest(true);
				st.playSound("ItemSound.quest_finish");
			}
			break;
		}
		
		return htmltext;
	}
	
	@Override
	public String onKill(L2Npc npc, L2PcInstance player, boolean isPet)
	{
		QuestState st = player.getQuestState(qn);
		if (st == null)
			return null;
		
		int cond = st.getInt("cond");
		
		if (cond == 1)
		{
			int qty = 0;
			int count = st.getQuestItemsCount(WOLF_CLAW);
			
			qty = st.getRandom(8);
			
			if (count + qty > 50)
				qty = 50 - count;
			else if (count + qty == 50)
			{
				st.playSound("ItemSound.quest_middle");
				st.set("cond", "2");
			}
			else
				st.playSound("ItemSound.quest_itemget");
			st.giveItems(WOLF_CLAW, qty);
		}
		
		return null;
	}
	
	public static void main(String[] args)
	{
		new Q264_KeenClaws(264, qn, "Keen Claws");
	}
}
